package com.vegetable.modules.service.outbound.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vegetable.common.constant.CommonConstant;
import com.vegetable.modules.entity.outbound.AllocationOrder;
import com.vegetable.modules.entity.outbound.AllocationOrderDetail;
import com.vegetable.modules.entity.outbound.AllocationOut;
import com.vegetable.modules.entity.outbound.AllocationOutDetail;
import com.vegetable.modules.mapper.outbound.AllocationOutDetailMapper;
import com.vegetable.modules.mapper.outbound.AllocationOutMapper;
import com.vegetable.modules.service.outbound.IAllocationOutService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 销售订货 服务类
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-27
 */
@Slf4j
@Service
public class AllocationOutServiceImpl extends ServiceImpl<AllocationOutMapper, AllocationOut> implements IAllocationOutService {
    @Autowired
    private AllocationOutDetailMapper allocationOutDetailMapper;
    /**
     * 新增调拨订单
     * @param po
     * @return
     */
    public int addOut(AllocationOrder po){
        List<AllocationOrderDetail> commodities = po.getCommodities();
        AllocationOut order = new AllocationOut();
        BeanUtils.copyProperties(po, order);
        order.setDocNo(CommonConstant.DBCK + baseMapper.getNextCode());
        order.setOrderId(po.getId());
        // 新增调拨订单
        this.baseMapper.insert(order);
        for(AllocationOrderDetail detail : commodities){
            AllocationOutDetail inDetail = new AllocationOutDetail();
            BeanUtils.copyProperties(detail, inDetail);
            inDetail.setOutOrderId(order.getId());
            allocationOutDetailMapper.insert(inDetail);
        }
        return 1;
    }
}
